﻿namespace DimensionsHelper.LanguageService.Syntax;

public class SyntaxTreeIncludeNode : Statement
{
    private WeakReference<SyntaxTree>? _tree;

    internal SyntaxTreeIncludeNode(SyntaxNode? parent, FileInclude includeNode) :
        base(SyntaxKinds.SyntaxTreeInclude, parent)
    {
        IncludeNode = includeNode;
    }


    /// <summary>
    ///     预处理的 #include 语法节点。
    /// </summary>
    public FileInclude IncludeNode { get; }


    /// <summary>
    ///     获取当前引入文件的语法树，如果尚未经过 Parser 解析语法树，返回<see langword="null" />
    /// </summary>
    /// <returns></returns>
    public SyntaxTree? GetSyntaxTree()
    {
        return _tree?.TryGetTarget(out SyntaxTree? tree) == true ? tree : null;
    }


    /// <summary>
    ///     更新文件的语法树
    /// </summary>
    public void UpdateSyntaxTree(SyntaxTree tree)
    {
        _tree = new WeakReference<SyntaxTree>(tree);
    }
}


public static partial class NodeFactory
{
    public static SyntaxTreeIncludeNode CreateSyntaxTreeIncludeNode(SyntaxNode? parent, FileInclude fileIncludeNode)
    {
        return new SyntaxTreeIncludeNode(parent, fileIncludeNode);
    }
}